import axios from 'axios'
import sysConfig from "@/config";
import tool from "@/utils/tool";
import {ElMessageBox, ElNotification} from "element-plus";
import router from "@/router";

let axiosInstance = axios.create({
    baseURL: sysConfig.API_URL,
    timeout: sysConfig.TIMEOUT
});


axiosInstance.interceptors.request.use(
    (config) => {
        let token = tool.cookie.get("TOKEN");
        if (token) {
            config.headers[sysConfig.TOKEN_NAME] = sysConfig.TOKEN_PREFIX + token
        }
        Object.assign(config.headers, sysConfig.HEADERS)
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);


axiosInstance.interceptors.response.use(
    (response) => {
        if (response.config.responseType === "blob") {
            return response;
        }

        if (response.config.unpack === true) {
            //如果不需要解包
            return response;
        }


        console.log("hhhhhhhhhh", response.data)
        return response.data
    },
    (error) => {
        if (error.response) {

            let msg = error.response.data?.msg;
            if (msg) {
                ElNotification.error({
                    title: '请求错误',
                    message: msg
                });
            } else if (error.response.status === 404) {
                ElNotification.error({
                    title: '请求错误',
                    message: "Status:404，正在请求不存在的服务器记录！"
                });
            } else if (error.response.status === 500) {
                // ElNotification.error({
                //     title: '请求错误',
                //     message: "Status:500，服务器发生错误！"
                // });
            } else if (error.response.status === 401) {
                ElMessageBox.confirm('当前用户已被登出或无权限访问当前资源，请尝试重新登录后再操作。', '无权限访问', {
                    type: 'error',
                    closeOnClickModal: false,
                    center: true,
                    confirmButtonText: '重新登录'
                }).then(() => {
                    router.replace({path: '/login'});
                }).catch(() => {
                })
            } else {
                ElNotification.error({
                    title: '请求错误',
                    message: error.message || `Status:${error.response.status}，未知错误！`
                });
            }
        } else {
            ElNotification.error({
                title: '请求错误',
                message: "请求服务器无响应！"
            });
        }

        return Promise.reject(error.response);
    }
);
export default axiosInstance
